package com.googlecode.sc2dm.register;

import com.googlecode.sc2dm.log.Logger;
import com.googlecode.sc2dm.register.util.*;
import junit.framework.TestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Jarle Hansen (jarle@jarlehansen.net)
 * Created: 7:52 PM - 9/18/11
 */
public class OnRegisteredTest extends TestCase {
    private DeviceRegistration deviceRegistration;
    private List<Class<?>> annotatedClasses;

    @Override
    protected void setUp() throws Exception {
        deviceRegistration = new DeviceRegistration();
        MessageStorageUtil.REG_ID.reset();

        annotatedClasses = new ArrayList<Class<?>>();
    }

    public void testOnRegisteredTwoArgs() throws IOException {
        annotatedClasses.add(AnnotatedTwoArgs.class);

        deviceRegistration.findAnnotations(annotatedClasses);
        deviceRegistration.onRegistered(null, "TEST-ID");

        assertEquals("TEST-ID", MessageStorageUtil.REG_ID.getIdentifier());
    }

    public void testOnRegisteredOneArgContext() throws IOException {
        annotatedClasses.add(AnnotatedOneArg1.class);

        deviceRegistration.findAnnotations(annotatedClasses);
        deviceRegistration.onRegistered(null, "TEST-ID");
    }

    public void testOnRegisteredOneArgRegId() throws IOException {
        annotatedClasses.add(AnnotatedOneArg2.class);

        deviceRegistration.findAnnotations(annotatedClasses);
        deviceRegistration.onRegistered(null, "TEST-ID2");

        assertEquals("TEST-ID2", MessageStorageUtil.REG_ID.getIdentifier());
    }

    public void testNotAnnotated() throws IOException {
        annotatedClasses.add(NotAnnotated.class);

        deviceRegistration.findAnnotations(annotatedClasses);
        deviceRegistration.onRegistered(null, "TEST-ID");
    }

    public void testPerformance() throws IOException {
        annotatedClasses.add(AnnotatedTwoArgs.class);

        deviceRegistration.findAnnotations(annotatedClasses);

        long start = System.currentTimeMillis();
        deviceRegistration.onRegistered(null, "TEST-ID");
        long end = System.currentTimeMillis();

        Logger.i("Time used: " + (end - start));
    }

}
